package com.example.jqying.androiddemo.recyclerView.activity;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.example.jqying.androiddemo.R;
import com.example.jqying.androiddemo.recyclerView.adapter.WaterfallAdapter;

public class WaterfallRecyclerActivity extends AppCompatActivity {


    private RecyclerView mRvWaterfall;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_waterfall_recycler);


        mRvWaterfall = (RecyclerView) findViewById(R.id.rv_waterfall);
        //设置瀑布流的管理器
        mRvWaterfall.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        mRvWaterfall.setAdapter(new WaterfallAdapter(WaterfallRecyclerActivity.this, new WaterfallAdapter.OnitemClickListener() {
            @Override
            public void onClick(int pos) {
                Toast.makeText(WaterfallRecyclerActivity.this,"Clicked:"+pos,Toast.LENGTH_SHORT).show();
            }
        }));
//        DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL);
//        divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.custom_waterfall_divider));
        MyDecoration divider = new MyDecoration();
        mRvWaterfall.addItemDecoration(divider);

    }

        class MyDecoration extends RecyclerView.ItemDecoration{

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int gap = getResources().getDimensionPixelSize(R.dimen.dividerHeight);
            outRect.set(gap,gap,gap,gap);



        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);

            //设置画笔的颜色
            c.drawColor(ContextCompat.getColor(WaterfallRecyclerActivity.this,R.color.colorFireRed));
        }


    }
}
